package com.zq.im.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import com.zq.im.BaseActivity;
import com.zq.im.R;
import com.zq.im.api.FriendsApi;
import com.zq.im.bean.BaseModel;
import com.zq.im.bean.FriendBean;
import com.zq.im.bean.NullBean;
import com.zq.im.bean.RequestedFriendListBean;
import com.zq.im.retrofit.BaseCallBack;
import com.zq.im.retrofit.RetrofitManager;
import com.zq.im.utils.SharePreferenceUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 搜索好友之后,添加好友
 * 好友请求列表,添加到通讯录
 */
public class FriendDetailActivity extends BaseActivity {

    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.add_friend)
    TextView addFriend;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_friend_result;
    }

    @Override
    protected void initImmersionBar() {
        ImmersionBar.with(this)
                .titleBarMarginTop(R.id.toolbar)
                .barColor(R.color.default_white)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();

        String action = intent.getAction();
        if (action != null && action.equals("fromFriendRequestListActivity")) { //来自好友请求列表
            RequestedFriendListBean bean = (RequestedFriendListBean) intent.getSerializableExtra("bean");
            if (bean != null) {
                Glide.with(this).load(bean.getSendFaceImage()).error(R.mipmap.icon).into(icon);
                name.setText(bean.getSendNickname());
                addFriend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        confirmRequest(bean.getSendUserId(), "1");
                    }
                });
            }

        } else if (action != null && action.equals("SearchFriendActivity")) {
            FriendBean friend = (FriendBean) intent.getSerializableExtra("friend");
            if (friend != null) {
                Glide.with(this).load(friend.getFaceImage()).error(R.mipmap.icon).into(icon);
                name.setText(friend.getNickname());
                addFriend.setText("发送请求");
                addFriend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        sendRequest(friend.getUsername());
                    }
                });
            }
        }

    }

    /**
     * 通过好友请求
     */
    void confirmRequest(String requestUserId, String type) {
        FriendsApi friendsApi = RetrofitManager.getRetrofit().create(FriendsApi.class);
        String userId = SharePreferenceUtils.getString("userId");
        Call<BaseModel<NullBean>> confirmRequest = friendsApi.confirmRequest(userId, requestUserId, type);
        confirmRequest.enqueue(new BaseCallBack<NullBean>() {
            @Override
            public void onSuccess(NullBean bean) {
                Toast.makeText(mActivity, "通过请求", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post("refreshFriendList");
                finish();
            }

            @Override
            public void onFailure(Throwable t) {

            }
        });
    }

    /**
     * 发送好友请求
     */
    void sendRequest(String friendUserName) {
        FriendsApi friendsApi = RetrofitManager.getRetrofit().create(FriendsApi.class);
        String userId = SharePreferenceUtils.getString("userId");
        Call<BaseModel<NullBean>> addFriendCall = friendsApi.addFriend(userId, friendUserName);
        addFriendCall.enqueue(new BaseCallBack<NullBean>() {
            @Override
            public void onSuccess(NullBean bean) {
                Toast.makeText(mActivity, "好友请求已经发送", Toast.LENGTH_LONG).show();
                finish();
            }

            @Override
            public void onFailure(Throwable t) {

            }
        });

    }
}